Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Replace anonymous types with lambdas #15043

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from

Conversation

iampopovich
Copy link
Contributor

@iampopovich iampopovich commented Jan 7, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

This pull request includes several changes to the implementation of various interfaces in the Selenium project. The main goal of these changes is to simplify the code by using lambda expressions instead of anonymous inner classes.

Description

Simplification of interface implementations:

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Replaced anonymous inner classes with lambda expressions for simplicity.

  • Updated AddHasCdp to use lambda for executeCdpCommand.

  • Updated AddHasLaunchApp to use lambda for launchApp.

  • Updated AddHasPermissions to use lambda for setPermission.

  • Updated AddHasDebugger to use lambda for attachDebugger.


Changes walkthrough 📝

Relevant files
Enhancement
AddHasCdp.java
Refactored `AddHasCdp` to use lambda expressions                 

java/src/org/openqa/selenium/chromium/AddHasCdp.java

  • Replaced anonymous inner class with a lambda expression.
  • Simplified executeCdpCommand implementation.
  • +7/-12   
    AddHasLaunchApp.java
    Refactored `AddHasLaunchApp` to use lambda expressions     

    java/src/org/openqa/selenium/chromium/AddHasLaunchApp.java

  • Replaced anonymous inner class with a lambda expression.
  • Simplified launchApp implementation.
  • +3/-7     
    AddHasPermissions.java
    Refactored `AddHasPermissions` to use lambda expressions 

    java/src/org/openqa/selenium/chromium/AddHasPermissions.java

  • Replaced anonymous inner class with a lambda expression.
  • Simplified setPermission implementation.
  • +5/-9     
    AddHasDebugger.java
    Refactored `AddHasDebugger` to use lambda expressions       

    java/src/org/openqa/selenium/safari/AddHasDebugger.java

  • Replaced anonymous inner class with a lambda expression.
  • Simplified attachDebugger implementation.
  • +1/-6     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 7, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The lambda implementation does not handle potential exceptions from executeMethod.execute() which could affect error reporting and debugging

    Map<String, Object> toReturn =
        (Map<String, Object>)
            executeMethod.execute(EXECUTE_CDP, Map.of("cmd", commandName, "params", parameters));
    
    return Map.copyOf(toReturn);

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 7, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add type checking and error handling for unsafe type casting operations

    Add error handling around the Map cast operation to handle potential
    ClassCastException if executeMethod.execute() returns an unexpected type.

    java/src/org/openqa/selenium/chromium/AddHasCdp.java [54-56]

    -Map<String, Object> toReturn =
    -    (Map<String, Object>)
    -        executeMethod.execute(EXECUTE_CDP, Map.of("cmd", commandName, "params", parameters));
    +Object result = executeMethod.execute(EXECUTE_CDP, Map.of("cmd", commandName, "params", parameters));
    +if (!(result instanceof Map)) {
    +  throw new WebDriverException("Expected CDP command to return a Map, got: " + result);
    +}
    +Map<String, Object> toReturn = (Map<String, Object>) result;
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical safety issue by adding proper type checking before casting, which could prevent runtime exceptions and improve error diagnostics in production.

    8
    General
    Validate enum-like string parameters before use to prevent invalid states

    Add input validation to ensure the permission value is one of the allowed states
    (likely 'granted', 'denied', or 'prompt').

    java/src/org/openqa/selenium/chromium/AddHasPermissions.java [60-65]

     return (name, value) -> {
       Require.nonNull("Permission name", name);
       Require.nonNull("Permission value", value);
    +  if (!Arrays.asList("granted", "denied", "prompt").contains(value)) {
    +    throw new IllegalArgumentException("Permission value must be one of: granted, denied, prompt");
    +  }
       executeMethod.execute(
           SET_PERMISSION, Map.of("descriptor", Map.of("name", name), "state", value));
     };
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important input validation to prevent invalid permission states, which could lead to runtime errors or unexpected behavior in the browser permissions system.

    7

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant